package 分类.链表;

public class K个一组翻转链表 {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode a,b;
        a=b=head;
        for (int i=0;i<k;i++){
            if(b==null) return head;
            b=b.next;
        }

        ListNode newHead=reverseK(a,b);
        a.next=reverseKGroup(b,k);
        return newHead;
    }

    ListNode reverseK(ListNode a,ListNode b){
        ListNode cur,pre,nxt;
        pre=null;
        cur=a;

        while (cur!=b){
            nxt=cur.next;
            cur.next=pre;
            pre=cur;
            cur=nxt;
        }
        return pre;
    }
}
